// pages/jm1/jm1.js
Page({
  data: {
    inputValue:'',
    thTime:'',
    CeSimage:'',
    imgyear:'',
    imgmonth:'',
    imgday:'',
    imghour:'',
    imgmin:'',
    dbData:[],
    thIndex: 0,
    thData1:[],
    thData2:[],
    thData3:[],
    thData4:[],
    thData5:[],
    dbImg:[],
    thImg:[],
    imIndex:0,
    ctime1:'',
    ctime2:'',
    ctime3:'',
    ctime4:'',
    ctime5:'',
  },
  onLoad(options) {
    if(options.inputValue){
      this.setData({
        inputValue:decodeURIComponent(options.inputValue)
      });
      this.submitData();
    }
  },
  showNext(){
    let newIndex = this.data.thIndex+5;
    if(newIndex >=this.data.dbData.length){
      newIndex = 0;
    }
    this.setData({
      thIndex:newIndex,
      thData1:this.data.dbData[newIndex],
      thData2:this.data.dbData[newIndex+1],
      thData3:this.data.dbData[newIndex+2],
      thData4:this.data.dbData[newIndex+3],
      thData5:this.data.dbData[newIndex+4],
      thTime:this.data.dbData[newIndex].timestamp.toString().slice(4,8),
      ctime1:this.data.dbData[newIndex].timestamp.toString().slice(8,14),
      ctime2:this.data.dbData[newIndex+1].timestamp.toString().slice(8,14),
      ctime3:this.data.dbData[newIndex+2].timestamp.toString().slice(8,14),
      ctime4:this.data.dbData[newIndex+3].timestamp.toString().slice(8,14),
      ctime5:this.data.dbData[newIndex+4].timestamp.toString().slice(8,14),
    });
  },
  showUp(){
     let newIndex = this.data.thIndex - 5;
    if(newIndex<0){
      newIndex=this.data.dbData.length -5;
    }
    this.setData({
      thIndex:newIndex,
      thData1:this.data.dbData[newIndex],
      thData2:this.data.dbData[newIndex+1],
      thData3:this.data.dbData[newIndex+2],
      thData4:this.data.dbData[newIndex+3],
      thData5:this.data.dbData[newIndex+4],
      thTime:this.data.dbData[newIndex].timestamp.toString().slice(4,8),
      ctime1:this.data.dbData[newIndex].timestamp.toString().slice(8,14),
      ctime2:this.data.dbData[newIndex+1].timestamp.toString().slice(8,14),
      ctime3:this.data.dbData[newIndex+2].timestamp.toString().slice(8,14),
      ctime4:this.data.dbData[newIndex+3].timestamp.toString().slice(8,14),
      ctime5:this.data.dbData[newIndex+4].timestamp.toString().slice(8,14),
    });
  },
  toNext(){
    let newIndex = this.data.imIndex +1;
    if(newIndex>=this.data.dbImg.length)
    { newIndex=0;}
    this.setData({
      imIndex:newIndex,
      CeSimage:this.data.dbImg[newIndex].picture_path.toString().slice(11,45),
      imgyear:this.data.dbImg[newIndex].picture_path.toString().slice(26,30),
      imgmonth:this.data.dbImg[newIndex].picture_path.toString().slice(30,32),
      imgday:this.data.dbImg[newIndex].picture_path.toString().slice(32,34),
      imghour:this.data.dbImg[newIndex].picture_path.toString().slice(35,37),
      imgmin:this.data.dbImg[newIndex].picture_path.toString().slice(37,39)
    })
  },
  toUp(){
    let newIndex = this.data.imIndex -1;
    if(newIndex<0)
    { newIndex=this.data.dbImg.length-1;}
    this.setData({
      imIndex:newIndex,
      CeSimage:this.data.dbImg[newIndex].picture_path.toString().slice(11,45),
      imgyear:this.data.dbImg[newIndex].picture_path.toString().slice(26,30),
      imgmonth:this.data.dbImg[newIndex].picture_path.toString().slice(30,32),
      imgday:this.data.dbImg[newIndex].picture_path.toString().slice(32,34),
      imghour:this.data.dbImg[newIndex].picture_path.toString().slice(35,37),
      imgmin:this.data.dbImg[newIndex].picture_path.toString().slice(37,39)
    })
  },
  submitData(){
    const inputValue = this.data.inputValue;
    wx.request({
      url: 'https://kaopu328.site:3000/rn',
      method:'POST',
      header:{'Content-Type':'application/json'},
      data:{inputData:inputValue},
      success:res=>{
        setTimeout(()=>{
          this.setData({
            dbData:res.data
          },()=>{
            console.log("shurul",this.data.dbData.length);
            this.setData({
              thData1:this.data.dbData[this.data.thIndex],
              thData2:this.data.dbData[this.data.thIndex+1],
              thData3:this.data.dbData[this.data.thIndex+2],
              thData4:this.data.dbData[this.data.thIndex+3],
              thData5:this.data.dbData[this.data.thIndex+4],
              thTime:this.data.dbData[this.data.thIndex].timestamp.toString().slice(4,8),
              ctime1:this.data.dbData[this.data.thIndex].timestamp.toString().slice(8.14),
              ctime2:this.data.dbData[this.data.thIndex+1].timestamp.toString().slice(8.14),
              ctime3:this.data.dbData[this.data.thIndex+2].timestamp.toString().slice(8.14),
              ctime4:this.data.dbData[this.data.thIndex+3].timestamp.toString().slice(8.14),
              ctime5:this.data.dbData[this.data.thIndex+4].timestamp.toString().slice(8.14)
            });
          });
        },1000);
        console.log(res.data)
      }
    })
    wx.request({
      url: 'https://kaopu328.site:3000/img',
      method:'POST',
      header:{'Content-Type':'application/json'},
      data:{inputData:inputValue},
      success:res=>{
        setTimeout(()=>{
          this.setData({
            dbImg:res.data
          },()=>{
            console.log("shuru2",this.data.dbImg.length);
            this.setData({
              thImg:this.data.dbImg[this.data.imIndex],
              CeSimage:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(11,45),
              imgyear:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(26,30),
              imgmonth:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(30,32),
              imgday:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(32,34),
              imghour:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(35,37),
              imgmin:this.data.dbImg[this.data.imIndex].picture_path.toString().slice(37,39),
            });
          });
        },1000);
        console.log(res.data)
      }
    })
  }
})